package com.dj.mingkong.portal.biz.repository.database.music;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dj.mingkong.common.constant.CommonConstants;
import com.dj.mingkong.common.infrastructure.base.dto.query.QueryVo;
import com.dj.mingkong.common.infrastructure.base.repository.BaseDatabaseRepository;
import com.dj.mingkong.common.infrastructure.eo.database.music.MusicEo;
import com.dj.mingkong.common.util.LocalDateTimeUtils;
import com.dj.mingkong.portal.biz.controller.music.vo.request.MusicQueryVo;
import com.dj.mingkong.portal.biz.repository.database.music.mapper.MusicMapper;
import com.dj.mingkong.portal.biz.util.WrapperUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.Arrays;


/**
 * 音乐Repository
 *
 * @author zenglonghui
 * @date: 2022/5/17 17:22
 */
@Repository
public class MusicRepository extends BaseDatabaseRepository<MusicMapper, MusicEo, QueryVo> {
    public Page<MusicEo> listPageByVo(MusicQueryVo queryVo) {
        final QueryWrapper<MusicEo> queryWrapper = Wrappers.query();
        queryWrapper.lambda().eq(MusicEo::getMusicianId, queryVo.getMusicianId())
                .like(StringUtils.isNotBlank(queryVo.getName()), MusicEo::getName, queryVo.getName())
                .in(StringUtils.isNotBlank(queryVo.getStatus()), MusicEo::getStatus,
                        Arrays.stream(queryVo.getStatus().split(CommonConstants.SPLIT_COMMA)).toList())
                .ge(StringUtils.isNotBlank(queryVo.getStartDate()), MusicEo::getCreateTime, queryVo.getStartDate())
                .le(StringUtils.isNotBlank(queryVo.getEndDate()), MusicEo::getCreateTime, queryVo.getEndDate());
        WrapperUtil.buildSort(queryWrapper, queryVo.getSort());
        return listPage(queryWrapper, queryVo.getPage(), queryVo.getPageSize());
    }

    public Long countByMusicianId(Long musicianId) {
        return lambdaQuery().
                eq(MusicEo::getMusicianId, musicianId)
                .ge(MusicEo::getCreateTime, LocalDateTimeUtils.getMondayForThisWeek())
                .le(MusicEo::getCreateTime, LocalDateTimeUtils.getSundayForThisWeek())
                .count();
    }
}
